/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.common.utils;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.NonNull;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * JSON辅助类
 * @author zpy
 * @version 1.0.0
 */
public final class JsonUtils {
    private static final ObjectMapper MAPPER = objectMapper();
    private JsonUtils(){}

    public static <T> T fromJson(String content,Class<T> type){
        try{
            return MAPPER.readValue(content,type);
        }catch (Exception ignore){
            return null;
        }
    }

    public static String toJson(Object content){
        if (Objects.isNull(content)) {
            return "";
        }
        try {
            return MAPPER.writeValueAsString(content);
        } catch (Exception ignore) {
            return "";
        }
    }

    public static <T> List<T> jsonToList(@NonNull String content, Class<T> type) {
        try {
            JavaType dataType = MAPPER.getTypeFactory().constructParametricType(List.class, type);
            return MAPPER.readValue(content, dataType);
        } catch (Exception ignore) {
            return new ArrayList<>();
        }
    }


    private static ObjectMapper objectMapper(){
        ObjectMapper mapper = new ObjectMapper();
        var javaTimeModule = new JavaTimeModule();
        var format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        var localDateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(format));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(format));

        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(localDateFormat));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(localDateFormat));
        mapper.registerModule(javaTimeModule);
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        return mapper;
    }
}